﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using Arch;
using InvoiceChecking.Data;
using InvoiceChecking.Data.SqlClient;
using InvoiceChecking.Entities;

public partial class Admin_ReportPrinting : System.Web.UI.Page
{

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        HttpCookie httpCookie = Request.Cookies["ICConnectionString"];
        if (httpCookie != null)
        {
            SqlNetTiersProvider provider = GlobalUtilities.CreateSunProvider(Server.MapPath("~") + "/Config.xml",
                                                                          httpCookie.Value);
            if (provider != null)
                DataRepository.LoadProvider(provider, true);
        }
    }
    protected void grv_BatchList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_BatchList.PageIndex = e.NewPageIndex;
        BindData();
    }
    protected void btn_Filter_Click(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        string whereExpression = BuildWhereExpression();
        int count;
        VList<SunReportPrinting> sunInvoices = DataRepository.SunReportPrintingProvider.GetPaged(whereExpression, "", 0, 1000, out count);
        if (count == 0)
            lbl_Error.Text = "No data. Please check the filter expression";
        else
        {
            lbl_Error.Text = "";
            grv_BatchList.RecordsCount = count;
            grv_BatchList.DataSource = sunInvoices;
            grv_BatchList.DataBind();
        }
    }

    private string BuildWhereExpression()
    {
        string whereExpression = "";
        if (!string.IsNullOrEmpty(tb_VoucherDateFrom.Text))
        {
            if (!string.IsNullOrEmpty(tb_VoucherDateTo.Text))
                whereExpression += string.Format("DatePrinted BETWEEN '{0} 00:00:00' AND '{1} 23:59:59'", tb_VoucherDateFrom.Text,
                                                    tb_VoucherDateTo.Text);
            else
                whereExpression += string.Format("DATEDIFF(d,DatePrinted,'{0}')", tb_VoucherDateFrom.Text);
        }
        return whereExpression;
    }
    protected void grv_BatchList_ExcelBeforeFormat(object sender, InvoiceChecking.Web.UI.ExcelFormatEventArgs e)
    {
        BindData();
    }
}
